package com.rem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rem.pojo.AclUser;
import com.rem.service.AclUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author remembrance
 * @version 1.0
 * @date 2021/11/22 14:17
 */
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    AclUserService aclUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> auth = AuthorityUtils
                .commaSeparatedStringToAuthorityList("role");
        QueryWrapper<AclUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        AclUser user = aclUserService.getOne(queryWrapper);
        return new User(user.getUsername(),new BCryptPasswordEncoder().encode(user.getPassword()),auth);
    }
}
